3 * Implements Special:Redirect
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
24 use MediaWiki\MediaWikiServices
;
27 * A special page that redirects to: the user for a numeric user id,
28 * the file for a given filename, or the page for a given revision id.
30 * @ingroup SpecialPage
33 class SpecialRedirect
extends FormSpecialPage
{
36 * The type of the redirect (user/file/revision)
38 * Example value: `'user'`
45 * The identifier/value for the redirect (which id, which file)
47 * Example value: `'42'`
53 function __construct() {
54 parent
::__construct( 'Redirect' );
60 * Set $mType and $mValue based on parsed value of $subpage.
61 * @param string $subpage
63 function setParameter( $subpage ) {
64 // parse $subpage to pull out the parts
65 $parts = explode( '/', $subpage, 2 );
66 $this->mType
= $parts[0];
67 $this->mValue
= $parts[1] ??
null;
71 * Handle Special:Redirect/user/xxxx (by redirecting to User:YYYY)
73 * @return Status A good status contains the url to redirect to
75 function dispatchUser() {
76 if ( !ctype_digit( $this->mValue
) ) {
77 // Message: redirect-not-numeric
78 return Status
::newFatal( $this->getMessagePrefix() . '-not-numeric' );
80 $user = User
::newFromId( (int)$this->mValue
);
81 $username = $user->getName(); // load User as side-effect
82 if ( $user->isAnon() ) {
83 // Message: redirect-not-exists
84 return Status
::newFatal( $this->getMessagePrefix() . '-not-exists' );
86 $userpage = Title
::makeTitle( NS_USER
, $username );
88 return Status
::newGood( $userpage->getFullURL( '', false, PROTO_CURRENT
) );
92 * Handle Special:Redirect/file/xxxx
94 * @return Status A good status contains the url to redirect to
96 function dispatchFile() {
98 $title = Title
::newFromTextThrow( $this->mValue
, NS_FILE
);
99 if ( $title && !$title->inNamespace( NS_FILE
) ) {
100 // If the given value contains a namespace enforce file namespace
101 $title = Title
::newFromTextThrow( Title
::makeName( NS_FILE
, $this->mValue
) );
103 } catch ( MalformedTitleException
$e ) {
104 return Status
::newFatal( $e->getMessageObject() );
106 $file = MediaWikiServices
::getInstance()->getRepoGroup()->findFile( $title );
108 if ( !$file ||
!$file->exists() ) {
109 // Message: redirect-not-exists
110 return Status
::newFatal( $this->getMessagePrefix() . '-not-exists' );
112 // Default behavior: Use the direct link to the file.
113 $url = $file->getUrl();
114 $request = $this->getRequest();
115 $width = $request->getInt( 'width', -1 );
116 $height = $request->getInt( 'height', -1 );
118 // If a width is requested...
119 if ( $width != -1 ) {
120 $mto = $file->transform( [ 'width' => $width, 'height' => $height ] );
122 if ( $mto && !$mto->isError() ) {
123 // ... change the URL to point to a thumbnail.
124 // Note: This url is more temporary as can change
125 // if file is reuploaded and has different aspect ratio.
126 $url = [ $mto->getUrl(), $height === -1 ?
301 : 302 ];
130 return Status
::newGood( $url );
134 * Handle Special:Redirect/revision/xxx
135 * (by redirecting to index.php?oldid=xxx)
137 * @return Status A good status contains the url to redirect to
139 function dispatchRevision() {
140 $oldid = $this->mValue
;
141 if ( !ctype_digit( $oldid ) ) {
142 // Message: redirect-not-numeric
143 return Status
::newFatal( $this->getMessagePrefix() . '-not-numeric' );
145 $oldid = (int)$oldid;
146 if ( $oldid === 0 ) {
147 // Message: redirect-not-exists
148 return Status
::newFatal( $this->getMessagePrefix() . '-not-exists' );
151 return Status
::newGood( wfAppendQuery( wfScript( 'index' ), [
157 * Handle Special:Redirect/page/xxx (by redirecting to index.php?curid=xxx)
159 * @return Status A good status contains the url to redirect to
161 function dispatchPage() {
162 $curid = $this->mValue
;
163 if ( !ctype_digit( $curid ) ) {
164 // Message: redirect-not-numeric
165 return Status
::newFatal( $this->getMessagePrefix() . '-not-numeric' );
167 $curid = (int)$curid;
168 if ( $curid === 0 ) {
169 // Message: redirect-not-exists
170 return Status
::newFatal( $this->getMessagePrefix() . '-not-exists' );
173 return Status
::newGood( wfAppendQuery( wfScript( 'index' ), [
179 * Handle Special:Redirect/logid/xxx
180 * (by redirecting to index.php?title=Special:Log&logid=xxx)
183 * @return Status A good status contains the url to redirect to
185 function dispatchLog() {
186 $logid = $this->mValue
;
187 if ( !ctype_digit( $logid ) ) {
188 // Message: redirect-not-numeric
189 return Status
::newFatal( $this->getMessagePrefix() . '-not-numeric' );
191 $logid = (int)$logid;
192 if ( $logid === 0 ) {
193 // Message: redirect-not-exists
194 return Status
::newFatal( $this->getMessagePrefix() . '-not-exists' );
196 $query = [ 'title' => 'Special:Log', 'logid' => $logid ];
197 return Status
::newGood( wfAppendQuery( wfScript( 'index' ), $query ) );
201 * Use appropriate dispatch* method to obtain a redirection URL,
202 * and either: redirect, set a 404 error code and error message,
203 * or do nothing (if $mValue wasn't set) allowing the form to be
206 * @return Status|bool True if a redirect was successfully handled.
208 function dispatch() {
209 // the various namespaces supported by Special:Redirect
210 switch ( $this->mType
) {
212 $status = $this->dispatchUser();
215 $status = $this->dispatchFile();
218 $status = $this->dispatchRevision();
221 $status = $this->dispatchPage();
224 $status = $this->dispatchLog();
230 if ( $status && $status->isGood() ) {
231 // These urls can sometimes be linked from prominent places,
233 $value = $status->getValue();
234 if ( is_array( $value ) ) {
235 list( $url, $code ) = $value;
240 if ( $code === 301 ) {
241 $this->getOutput()->setCdnMaxage( 60 * 60 );
243 $this->getOutput()->setCdnMaxage( 10 );
245 $this->getOutput()->redirect( $url, $code );
249 if ( !is_null( $this->mValue
) ) {
250 $this->getOutput()->setStatusCode( 404 );
258 protected function getFormFields() {
259 $mp = $this->getMessagePrefix();
261 // subpage => message
262 // Messages: redirect-user, redirect-page, redirect-revision,
263 // redirect-file, redirect-logid
264 'user' => $mp . '-user',
265 'page' => $mp . '-page',
266 'revision' => $mp . '-revision',
267 'file' => $mp . '-file',
268 'logid' => $mp . '-logid',
273 'label-message' => $mp . '-lookup', // Message: redirect-lookup
275 'default' => current( array_keys( $ns ) ),
277 foreach ( $ns as $n => $m ) {
278 $m = $this->msg( $m )->text();
279 $a['type']['options'][$m] = $n;
283 'label-message' => $mp . '-value' // Message: redirect-value
285 // set the defaults according to the parsed subpage path
286 if ( !empty( $this->mType
) ) {
287 $a['type']['default'] = $this->mType
;
289 if ( !empty( $this->mValue
) ) {
290 $a['value']['default'] = $this->mValue
;
296 public function onSubmit( array $data ) {
297 if ( !empty( $data['type'] ) && !empty( $data['value'] ) ) {
298 $this->setParameter( $data['type'] . '/' . $data['value'] );
301 /* if this returns false, will show the form */
302 return $this->dispatch();
305 public function onSuccess() {
306 /* do nothing, we redirect in $this->dispatch if successful. */
309 protected function alterForm( HTMLForm
$form ) {
310 /* display summary at top of page */
311 $this->outputHeader();
312 // tweak label on submit button
313 // Message: redirect-submit
314 $form->setSubmitTextMsg( $this->getMessagePrefix() . '-submit' );
315 /* submit form every time */
316 $form->setMethod( 'get' );
319 protected function getDisplayFormat() {
324 * Return an array of subpages that this special page will accept.
326 * @return string[] subpages
328 protected function getSubpagesForPrefixSearch() {
341 public function requiresWrite() {
348 public function requiresUnblock() {
352 protected function getGroupName() {